package components
{
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.controls.Button;
	import mx.controls.Label;
	import mx.controls.TextInput;
	import mx.events.ResizeEvent;
	
	import nl.wv.extenders.panel.SuperPanel;
	
	import tools.*;
	
	public class NewRoom{
		private var panel:SuperPanel = new SuperPanel();
		private var label1:Label = new Label();
		private var label2:Label = new Label();
		private var textInput1:TextInput = new TextInput();
		private var textInput2:TextInput = new TextInput();
		private var btn:Button = new Button();
		private var service:Mysql = new Mysql();
		private var service1:Mysql = new Mysql();
		private var tool:Tool = new Tool();
		
		public function NewRoom(p:SuperPanel){
			panel = p;
			label1.x = 10;
			label1.y = 27;
			label1.height = 25;
			label1.text = "*Naziv: ";
			label2.x = 10;
			label2.y = 57;
			label2.height = 25;
			label2.text = "Sifra: ";
			textInput1.x = 60;
			textInput1.y = 27;
			textInput1.width = panel.width - 90;
			textInput1.height = 20; 
			textInput2.x = 60;
			textInput2.y = 57;
			textInput2.width = panel.width - 90;
			textInput2.height = 20;
			textInput2.displayAsPassword = true;
			btn.x = 10;
			btn.y = 87;
			btn.width = panel.width -40;
			btn.height = 20;
			btn.label = "Kreiraj sobu";
			panel.addChild(label1);
			panel.addChild(label2);
			panel.addChild(textInput1);
			panel.addChild(textInput2);
			panel.addChild(btn);
			panel.addEventListener(ResizeEvent.RESIZE, onResize);
			btn.addEventListener(MouseEvent.CLICK, onClickAdd);
		}
		
		public function onClickAdd(e:MouseEvent):void{
            service.sql = {type:"INSERT", into:"soba", columns:"naziv, sifra, zadnjaIzmjenaChat, zadnjaIzmjenaUsers", values:"@"+textInput1.text+"@,@"+textInput2.text+"@,@"+tool.uniqueNumber()+"@,@"+tool.uniqueNumber()+"@" }; 
			service.addEventListener(service.MYSQL_RESULT, onAddRoom);
 			service.startService();
		}
		
		public function onAddRoom(e:Event):void{
			service1.sql = {type:"UPDATE", tables:"podesavanja p", s1:"zadnjaIzmjenaRooms="+tool.uniqueNumber(), where:"" }
			service1.addEventListener(service1.MYSQL_RESULT, onChanges);
			service1.startService();
		}
		
		public function onChanges(e:Event):void{
			panel.closePanel();
		}
		
		public function onResize(e:ResizeEvent):void{
			textInput1.width = panel.width - 90;
			textInput2.width = panel.width - 90;
			btn.width = panel.width -40;
		}
	}
}